<?php
class Malbumdetail extends CI_Model{
	function __construct(){
		parent::__construct();
	}
	
	function loadAlbumDetail($albumid){
		$sql = "select * 
				from tbalbumdetail
				where albumid=?
				order by id ";
		$query = $this->db->query($sql,array("$albumid"));
		return $query->result();
	}
	function getCount($albumid){
		$sql = "select count(1) num
				from tbalbumdetail
				where albumid = ?";
		$query = $this->db->query($sql,array("$albumid"));
		foreach ($query->result() as $row){
			return $row->num;
		}
		return 0;
	}
	function saveAlbumDetail($albumid,$url,$uid){
		$sql = "insert into tbalbumdetail(albumid,url,createtime,status,uid) values(?,?,now(),1,?)";
		$query = $this->db->query($sql,array("$albumid","$url",$uid));
		return $this->db->affected_rows();
	}

    /**
     * 获取N个相册里的一张相片
     * @param $albumid
     * @return mixed
     */
    function getAlbumDetail($albumid,$isCover=0){
        if(is_array($albumid)){
            $albumid = implode(',',$albumid);
        }
        $where = '';
        $where .= ' `albumid` in('.$albumid.') ';
        if($isCover){
            $where .= ' and `is_cover`=1 ';
        }

        $sql = "select * from tbalbumdetail where $where  group by `albumid`";

        $query = $this->db->query($sql);
        $item = array();
        if($query->num_rows() > 0){

            foreach($query->result() as $value){
                $item[$value->albumid] = $value->url;
            }
        }

        return $item;

    }

    /**
     * 获取所有相片
     * @param $pageSize
     * @param $order
     */
    function getAllPhoto($id,$pageSize,$order)
    {
        $sqlCount = $this->db->query("select `id` from `tbalbumdetail` ");
        $count = $sqlCount->num_rows();

        $sql = "select * from `tbalbumdetail` where `uid`=$id order by $order limit ";

        $query = $this->db->pager($count,$pageSize,$sql);

        //给每张图定一个高度
        foreach($query['data'] as &$value){
            $value['height'] = $this->getHeight();
        }

        return $query;
    }

    /**
     * 图片随机高度
     * @return mixed
     */
    function getHeight()
    {
        $heightArr = array(285,287,326,314,315,304,323,350,276,294,266,282);
        $heightCount = count($heightArr)-1;
        $r = mt_rand(0,$heightCount);
        $height = $heightArr[$r];

        return $height;
    }

    function markLike($uid,$photoId)
    {
        $this->Mphotodetail->installPhotoDetail($photoId,$uid);

        $sql = "update `tbalbumdetail` set `like_count`=`like_count`+1
				where id=?";
        $this->db->query($sql,array($photoId));
        return $this->db->affected_rows();
    }

}
?>